package com.woniuxy.enjoylife.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.enjoylife.entity.Activity;
import com.woniuxy.enjoylife.entity.Activity_comment;
import com.woniuxy.enjoylife.entity.dto.*;
import com.woniuxy.enjoylife.mapper.ActivityCommentMapper;
import com.woniuxy.enjoylife.mapper.ActivityMapper;
import com.woniuxy.enjoylife.mapper.CommunityMapper;
import com.woniuxy.enjoylife.mapper.UserMapper;
import com.woniuxy.enjoylife.service.ActivityService;
import com.woniuxy.enjoylife.utils.PageResult;
import com.woniuxy.enjoylife.utils.PageUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper,Activity> implements ActivityService {

    @Resource
    private ActivityMapper activityMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private CommunityMapper communityMapper;

    @Resource
    private ActivityCommentMapper activityCommentMapper;


    //查询所有活动
    @Override
    public PageResult<ActivityShowDto> selectActivity(int page, int size,int userid) {
        //
        List<ActivityShowDto> activityList = new ArrayList<>();
        //分页信息
        Page<Activity> activityPage = PageHelper.startPage(page, size);

        //查询活动
        List<Activity> activities = activityMapper.selectActivity();

        //遍历获取数据
        for (Activity activity:activities){
            //活动表获取
            Integer aId = activity.getId();
            //发起者
            Integer aUid = activity.getUid();
            //小区
            Integer aCid = activity.getCid();
            //活动名称
            String aName = activity.getName();
            //活动地址
            String aAddr = activity.getAddress();
            //活动时间
            LocalDateTime aTime = activity.getTime();
            String aImage = activity.getImage();
            String aContent = activity.getContent();
            Integer aResult =activity.getResult();
            //当前用户能否操作该条活动
            boolean isupdate = activity.getUid().equals(userid);

            //去用户表获取发布者的名字
            String userName = userMapper.selectById(aUid).getAccount();

            //获取小区名字
            String communitName = communityMapper.findNameByCid(aCid).getName();

            //获取活动报名的状态
            if (activity.getUid()==userid){
                aResult.toString();
                break;
            }

            //查询当前活动有关的所有评论
            List<Activity_comment> activityComments = activityCommentMapper.selectComment(aId);

            //封装活动相关数据
            ActivityShowDto activityShowDto = new ActivityShowDto();
            activityShowDto.setAId(aId);
            activityShowDto.setAUid(aUid);
            activityShowDto.setACid(aCid);
            activityShowDto.setAName(aName);
            activityShowDto.setAAddr(aAddr);
            activityShowDto.setATime(aTime);
            activityShowDto.setAImage(aImage);
            activityShowDto.setAContent(aContent);
            activityShowDto.setAResult(aResult);
            activityShowDto.setIsUpdate(isupdate);  //是否可以修改

            //用户表的数据
            activityShowDto.setUserName(userName);

            //小区数据
            activityShowDto.setCName(communitName);

            //评论表的数据（所有评论）
            activityShowDto.setCommentList(activityComments);

            //
            activityList.add(activityShowDto);
        }

        PageResult<ActivityShowDto> pageResult = new PageResult<>();
        pageResult.setCurrentPage(page);
        pageResult.setPageSize(size);
        pageResult.setTotal(activityPage.getTotal());
        pageResult.setTotalPage(activityPage.getPageNum());
        pageResult.setData(activityList);

        return pageResult;
    }

    //查找单个用户的活动(id,模糊查询)
    @Override
    public List<ActivityShowDto> selectOneActivity(Activity activity) {

        return null;
    }

    //新增
    @Override
    public int addActivity(Activity activity) {
        return activityMapper.addActivity(activity);
    }

    //修改
    @Override
    public int updateActivity(Activity activity) {
        return activityMapper.updateActivity(activity);
    }

    //删除
    @Override
    public int deleteActivity(int id) {
        return activityMapper.deleteActivity(id);
    }






    //根据小区id获取小区活动以及发起人，并分页
    @Override
    public PageResult<List<ActivityDto>> findActivitysByCid(int cid,int current,int size) {
        //分页信息
        PageHelper.startPage(current,size);
        List<ActivityDto> list = activityMapper.findActivitysByCid(cid);
        PageInfo<ActivityDto> pageInfo = new PageInfo(list);
        PageResult pageResult = PageUtil.setPageResult2(pageInfo);
        return pageResult;
    }

    //通过活动id去查当前活动信息以及发起者，参与者
    @Override
    public ActivityUsersDto findActivityUsersDtoById(int id) {
        return activityMapper.findActivityUsersDtoById(id);
    }

    //通过用户id去中间表删除该用户参加的该活动
    @Override
    public boolean delByUid(int uid, int aid) {
        return activityMapper.delByUid(uid,aid) > 0 ? true : false;
    }

    //根据活动id删除（将status改为1）
    @Override
    public boolean del(int id) {
        Activity activity = new Activity().setId(id).setStatus(1);
        return activityMapper.updateById(activity) > 0 ? true : false;
    }

    //小区发起活动
    @Override
    public boolean communityAddActivity(Activity activity) {
        return activityMapper.insert(activity) >0 ? true : false;
    }

    //平台活动管理，查询所有小区的活动
    @Override
    public PageResult<List<ActivityCommunityDto>> platFindAllActivity(int current, int size) {
        //分页信息
        PageHelper.startPage(current,size);
        List<ActivityCommunityDto> list = activityMapper.platFindAllActivity();
        PageInfo<ActivityCommunityDto> pageInfo = new PageInfo<>(list);
        PageResult pageResult = PageUtil.setPageResult2(pageInfo);
        return pageResult;
    }

    //通过用户id数组遍历批量删除用户参与活动
    @Override
    public boolean delBatchByUid(int[] ids,int aid){
        return activityMapper.delBatchByUid(ids,aid) > 0 ? true : false;
    }

    //用户查询自己发表的活动(用户id为活动表中发表人id)
    @Override
    public PageResult<List<ActivityUsersDto>> userFindOwnActivity(int uid, int current, int size) {
        PageHelper.startPage(current,size);
        List<ActivityUsersDto> list = activityMapper.userFindOwnActivity(uid);
        PageInfo<ActivityUsersDto> pageInfo = new PageInfo<>(list);
        PageResult pageResult = PageUtil.setPageResult2(pageInfo);
        return pageResult;
    }

    //用户查询自己参与的活动（去用户-活动中间表查询）
    @Override
    public PageResult<List<ActivityUsersDto>> userFindJoinActivity(int uid, int current, int size) {
        PageHelper.startPage(current,size);
        List<ActivityUsersDto> list = activityMapper.userFindJoinActivity(uid);
        PageInfo<ActivityUsersDto> pageInfo = new PageInfo<>(list);
        PageResult pageResult = PageUtil.setPageResult2(pageInfo);
        return pageResult;
    }

    //根据活动id去查询该活动的评论及用户
    @Override
    public PageResult<List<ActivityCommentUserDto>> findActivityCommentByAid(int aid, int current, int size) {
        PageHelper.startPage(current,size);
        List<ActivityCommentUserDto> list = activityMapper.findActivityCommentByAid(aid);
        PageInfo<ActivityCommentUserDto> pageInfo = new PageInfo<>(list);
        PageResult pageResult = PageUtil.setPageResult2(pageInfo);
        return pageResult;
    }

    @Override
    public ActivityUsersDto findActivityAndUserById(Integer id) {
        ActivityUsersDto activityAndUserById = activityMapper.findActivityAndUserById(id);


        return activityAndUserById;
    }
}
